package com.sms.controller;

import com.sms.pojo.ClassInfo;
import com.sms.pojo.CourseInfo;
import com.sms.pojo.CourseResult;
import com.sms.pojo.StudentInfo;
import com.sms.service.*;
import com.sms.utils.RunEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    ClassInfoService classInfoService;
    @Autowired
    CourseResultService courseResultService;
    @Autowired
    PersonInfoService personInfoService;
    @Autowired
    StudentInfoService studentInfoService;
    @Autowired
    CourseInfoService courseInfoService;
    //进入课程信息页面
    @RequestMapping("/indexUI")
    public String indexUI(HttpSession session,Model model) {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        List<CourseResult> courseResults = courseResultService.selectCourseResultBySid(student.getSid());
        model.addAttribute("courseResults",courseResults);
        return "student_indexUI";
    }
    //进入个人信息页面
    @RequestMapping("/information")
    public String information(HttpSession session,Model model) {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        ClassInfo classInfo = classInfoService.selectClassOne(student.getClassid());
        model.addAttribute("classInfo",classInfo);
        return "student_information";
    }
    //提交个人信息操作
    @RequestMapping("/Submitindividual")
    public String Submitindividual(StudentInfo studentInfo,HttpSession session,Model model) {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        ClassInfo classInfo = classInfoService.selectClassOne(student.getClassid());
        model.addAttribute("classInfo",classInfo);
        StudentInfo sz = (StudentInfo) session.getAttribute("student");
        sz.setPhone(studentInfo.getPhone());
        sz.setPsw(studentInfo.getPsw());
        studentInfoService.updateStudent(sz);
        session.setAttribute("student", sz);
        return "student_information";
    }
    //学生选课页面
    @RequestMapping("/courses")
    public String courses(HttpSession session, Model model) {
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("student");
        List<CourseInfo> courseInfos = courseResultService.pickCourses(studentInfo.getSid());
        model.addAttribute("courseInfos", courseInfos);
        return "student_courses";
    }
    //学生选课操作
    @RequestMapping("/Courseselection")
    public String Courseselection(int courseId, HttpSession session) {
        CourseResult courseResult = new CourseResult();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("student");
        CourseInfo courseInfo = courseInfoService.selectCourseOne(courseId);
        courseResult.setSid(studentInfo.getSid());
        courseResult.setCourseid(courseId);
        courseResult.setCoursename(courseInfo.getCourseName());
        courseResult.setName(studentInfo.getSname());
        courseResult.setSchoolyear("1");
        courseResultService.insertCourseResult(courseResult);
        return "redirect:/courses";
    }
    //上传作业页面
    @RequestMapping("/submit")
    public String submit(Model model, HttpSession session) {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        List<CourseResult> courseResults = courseResultService.selectCourseResultBySid(student.getSid());
        model.addAttribute("courseResults",courseResults);
        return "student_submit";
    }
    //提交作业页面
    @RequestMapping("/upload")
    public String upload(int courseId,Model model,HttpSession session) {
        CourseInfo courseInfo = courseInfoService.selectCourseOne(courseId);
        model.addAttribute("msgs",session.getAttribute("msg"));
        session.removeAttribute("msg");
        model.addAttribute("courseInfo",courseInfo);
        return "stduent_upload";
    }
    //学生上传作业操作
    @RequestMapping("/refer")
    public String refer(HttpServletRequest request,HttpSession session, MultipartFile word, Integer courseId) throws Exception {
        // 无所需接口
        session.removeAttribute("msg");
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        String path = RunEnvironment.getPath() + "static/files/";
        if(word.getOriginalFilename().endsWith("doc")){
            String fileName = student.getSid()+"_"+courseId+".doc";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdir();
            }
            File file = new File(path+fileName);
            System.out.println(file.getAbsolutePath());
            word.transferTo(file.toPath());
        }else {
            session.setAttribute("msg","文件格式错误，只允许.doc文件");
            return "redirect:/upload?courseId="+courseId;
        }
        return "redirect:/submit";
    }
    //个人成绩页面
    @RequestMapping("/achievement")
    public String achievement(Model model, HttpSession session) {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        List<CourseResult> courseResults = courseResultService.selectCourseResultBySid(student.getSid());
        model.addAttribute("courseResults",courseResults);
        return "student_achievement";
    }


}
